package com.nf.health.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nf.health.app.R;
import com.nf.health.app.models.PersonalDocModel;
import com.nf.health.app.utils.ViewUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Jason on 2016/6/23.
 */
public class MyPersonalDocAdapter extends BaseAdapter{
    private Activity context;
    private List<PersonalDocModel> listHealthy;

    public MyPersonalDocAdapter(Activity context,List<PersonalDocModel> mySportList) {
        this.context = context;
        this.listHealthy = mySportList;
    }

    @Override
    public int getCount() {
        if(listHealthy != null){
            return listHealthy.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return listHealthy.get(position);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_personal_doc, null);
            holder = new ViewHolder(context,convertView);
            holder.tv_item_doc_name = ViewUtil.getView(convertView,R.id.tv_item_doc_name);
            holder.iv_personal_doc_item = ViewUtil.getView(convertView,R.id.iv_personal_doc_item);
            holder.tv_item_doc_type = ViewUtil.getView(convertView,R.id.tv_item_doc_type);
            holder.tv_item_doc_job = ViewUtil.getView(convertView,R.id.tv_item_doc_job);
            holder.tv_item_doc_address = ViewUtil.getView(convertView,R.id.tv_item_doc_address);
            holder.tv_item_doc_date = ViewUtil.getView(convertView,R.id.tv_item_doc_date);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final PersonalDocModel model = listHealthy.get(position);
        Glide.with(context) .load(model.getHimg()).placeholder(R.drawable.default_head).into(holder.iv_personal_doc_item);
        holder.btn_item_person_ask = ViewUtil.getView(convertView,R.id.btn_item_person_ask);
        holder.tv_item_doc_name.setText(TextUtils.isEmpty(model.getNick()) ? "" : model.getNick());
        holder.tv_item_doc_type.setText(TextUtils.isEmpty(model.getDepartment()) ? "" : model.getDepartment());
        holder.tv_item_doc_job.setText(TextUtils.isEmpty(model.getProfessional()) ? "" : model.getProfessional());
        holder.tv_item_doc_address.setText(TextUtils.isEmpty(model.getHname()) ? "" :model.getHname());
        holder.tv_item_doc_date.setText(TextUtils.isEmpty(model.getDays()) ? ("有效期0天") : ("有效期"+model.getDays()+"天"));

        if(model.getStatus().equals("0")){//开始咨询
            holder.btn_item_person_ask.setText("开始咨询");
            holder.btn_item_person_ask.setBackgroundResource(R.drawable.shade_normble_persional);
            holder.btn_item_person_ask.setTextColor(context.getResources().getColor(R.color.white));
            holder.btn_item_person_ask.setEnabled(true);
            holder.btn_item_person_ask.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClick(position, "0");
                    }
                }
            });
        }else  if(model.getStatus().equals("1")){//咨询
          holder.btn_item_person_ask.setBackgroundResource(R.drawable.shade_normble_persional);
            holder.btn_item_person_ask.setTextColor(context.getResources().getColor(R.color.white));
            holder.btn_item_person_ask.setEnabled(true);
            holder.btn_item_person_ask.setText("咨  询");
            holder.btn_item_person_ask.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClick(position, "1");
                    }
                }
            });
        } if(model.getStatus().equals("2")){//已结束
            holder.btn_item_person_ask.setClickable(false);
            holder.btn_item_person_ask.setEnabled(false);
            holder.btn_item_person_ask.setBackgroundResource(R.drawable.shade_check_persional);
            holder.btn_item_person_ask.setTextColor(context.getResources().getColor(R.color.black8));
            holder.btn_item_person_ask.setText("已结束");
        }
        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.iv_personal_doc_item)
        com.hyphenate.easeui.widget.CircularImage iv_personal_doc_item;

        @Bind(R.id.tv_item_doc_name)
        TextView tv_item_doc_name;

        @Bind(R.id.tv_item_doc_type)
        TextView tv_item_doc_type;

        @Bind(R.id.tv_item_doc_job)
        TextView tv_item_doc_job;
        @Bind(R.id.tv_item_doc_address)
        TextView tv_item_doc_address;
        @Bind(R.id.tv_item_doc_date)
        TextView tv_item_doc_date;
        @Bind(R.id.btn_item_person_ask)
        Button btn_item_person_ask;

       ViewHolder(Activity act ,View view) {
//            ButterKnife.bind(act, view);
        }
    }

    private OnMyClickListener listener;
    public void setOnMyClickerListener(OnMyClickListener listener){
        this.listener = listener;
    }
    public void setMynotifyDataChanged( List<PersonalDocModel> listHealthy){
        this.listHealthy = listHealthy;
        notifyDataSetChanged();
    }
}
